(function(jQ) {
	var flowEditor = {}; 
	flowEditor.isSelect = false;//--是否框选
	flowEditor.isDrag = false;//--是否拖拽
	flowEditor.root = null;//--包含画布的div
	flowEditor.mouseIsMove = false;//鼠标是否在拖拽
	//--关于矩形和路径的配置
	flowEditor.config = {
		editable : true,
		lineHeight : 15,
		basePath : "",
		rect : {
			attr : {
				x : 10,
				y : 10,
				width : 100,
				height : 50,
				r : 5,
				fill : "90-#fff-#C0C0C0",
				stroke : "#000",
				"stroke-width" : 1
			},
			showType : "image&text",
			type : "state",
			name : {
				text : "state",
				"font-style" : "italic"
			},
			text : {
				text : "状态",
				"font-size" : 13
			},
			margin : 5,//外围激活圈离元素的距离
			props : [],
			img : {}
		},
		path : {
			attr : {
				path : {
					path : "M10 10L100 100",
					stroke : "#808080",
					fill : "none",
					"stroke-width" : 2
				},
				arrow : {
					path : "M10 10L10 10",
					stroke : "#808080",
					fill : "#808080",
					"stroke-width" : 2,
					radius : 4
				},
				fromDot : {
					width : 5,
					height : 5,
					stroke : "#fff",
					fill : "#000",
					cursor : "move",
					"stroke-width" : 2
				},
				toDot : {
					width : 5,
					height : 5,
					stroke : "#fff",
					fill : "#000",
					cursor : "move",
					"stroke-width" : 2
				},
				bigDot : {
					width : 5,
					height : 5,
					stroke : "#fff",
					fill : "#000",
					cursor : "move",
					"stroke-width" : 2
				},
				smallDot : {
					width : 5,
					height : 5,
					stroke : "#fff",
					fill : "#000",
					cursor : "move",
					"stroke-width" : 3
				}
			},
			text : {
				text : "TO {to}",
				cursor : "move",
				background : "#000"
			},
			textPos : {
				x : 0,
				y : -10
			},
			props : {
				text : {
					name : "text",
					label : "显示",
					value : "",
					editor : function() {
						return new flowEditor.editors.textEditor();
					}
				}
			}
		},
		tools : {
			attr : {
				left : 10,
				top : 10
			},
			pointer : {},
			path : {},
			states : {},
			save : {
				onclick : function(c) {
					alert(c);
				}
			},
			xmlSrc:{
				onclick : function(c) {
					alert(c);
				}
			},
			jsonSrc:{
				onclick : function(c) {
					alert(c);
				}
			}
		},
		props : {
			attr : {
				top : 35,//--sy
				right : 55
			},
			props : {}
		},
		restore : "",
		activeRects : {
			rects : [],
			rectAttr : {
				stroke : "#ff0000",
				"stroke-width" : 2
			}
		},
		historyRects : {
			rects : [],
			pathAttr : {
				path : {
					stroke : "#00ff00"
				},
				arrow : {
					stroke : "#00ff00",
					fill : "#00ff00"
				}
			}
		}
	};
	//--通用工具
	flowEditor.util = {
		isLine : function(g, f, e) {// 判断三个点是否在一条线上
			var d, c;
			if ((g.x - e.x) == 0) {
				d = 1;
			} else {
				d = (g.y - e.y) / (g.x - e.x);
			}
			c = (f.x - e.x) * d + e.y;
			if ((f.y - c) < 10 && (f.y - c) > -10) {
				f.y = c;
				return true;
			}
			return false;
		},
		center : function(d, c) {// 中点坐标
			return {
				x : (d.x - c.x) / 2 + c.x,
				y : (d.y - c.y) / 2 + c.y
			};
		},
		nextId : (function() {// id生成器
			var c = 0;
			return function() {
				return ++c;
			};
		})(),

		connPoint : function(j, d) {
			var c = d, e = {
				x : j.x + j.width / 2,
				y : j.y + j.height / 2
			};
			var l = (e.y - c.y) / (e.x - c.x);
			l = isNaN(l) ? 0 : l;
			var k = j.height / j.width;
			var h = c.y < e.y ? -1 : 1, f = c.x < e.x ? -1 : 1, g, i;
			
			
			if (Math.abs(l) >= k && h == -1) {
				g = e.y - j.height / 2;
				i = e.x + h * j.height / 2 / l;
			} else {
				if (Math.abs(l) >= k && h == 1) {
					g = e.y + j.height / 2;
					i = e.x + h * j.height / 2 / l;
				} else {
					if (Math.abs(l) < k && f == -1) {
						g = e.y + f * j.width / 2 * l;
						i = e.x - j.width / 2;
					} else {
						if (Math.abs(l) < k && f == 1) {
							g = e.y + j.width / 2 * l;
							i = e.x + j.width / 2;
						}
					}
				}
			}
			return {
				x : i,
				y : g
			};
		},
		arrow : function(l, k, d) {
			var g = Math.atan2(l.y - k.y, k.x - l.x) * (180 / Math.PI);
			var h = k.x - d * Math.cos(g * (Math.PI / 180));
			var f = k.y + d * Math.sin(g * (Math.PI / 180));
			var e = h + d * Math.cos((g + 120) * (Math.PI / 180));
			var j = f - d * Math.sin((g + 120) * (Math.PI / 180));
			var c = h + d * Math.cos((g + 240) * (Math.PI / 180));
			var i = f - d * Math.sin((g + 240) * (Math.PI / 180));
			return [ k, {
				x : e,
				y : j
			}, {
				x : c,
				y : i
			} ];
		}
	};
	flowEditor.rect = function(rectConf, raphael) {
		var u = this, id = flowEditor.util.nextId(), _state = jQ.extend(true,
				{}, flowEditor.config.rect, rectConf), _raphael = raphael, t, e, n, f, x, v;
		if(_state.text.text != 'start' && _state.text.text != "end"){
			_state.text.text = _state.text.text + id;
		}
		id = "rect" + id;
		t = _raphael.rect(_state.attr.x, _state.attr.y, _state.attr.width,
				_state.attr.height, _state.attr.r).hide().attr(_state.attr);
		e = _raphael.image(flowEditor.config.basePath + _state.img.src,
				_state.attr.x + _state.img.width / 2,
				_state.attr.y + (_state.attr.height - _state.img.height) / 2,
				_state.img.width, _state.img.height).hide();
		n = _raphael.text(
				_state.attr.x + _state.img.width
						+ (_state.attr.width - _state.img.width) / 2,
				_state.attr.y + flowEditor.config.lineHeight / 2,
				_state.name.text).hide().attr(_state.text);
		f = _raphael.text(
				_state.attr.x + _state.img.width
						+ (_state.attr.width - _state.img.width) / 2,
				_state.attr.y
						+ (_state.attr.height - flowEditor.config.lineHeight)
						/ 2 + flowEditor.config.lineHeight, _state.text.text)
				.hide().attr(_state.text);
		t.drag(function(r, o) {
			A(r, o);
		}, function() {
			z();
		}, function() {
			l();
		});
		e.drag(function(r, o) {
			A(r, o);
		}, function() {
			z();
		}, function() {
			l();
		});
		n.drag(function(r, o) {
			A(r, o);
		}, function() {
			z();
		}, function() {
			l();
		});
		f.drag(function(r, o) {
			A(r, o);
		}, function() {
			z();
		}, function() {
			l();
		});
        //拖动元素调用的方法
		var A = function(F, r) {
			if (!flowEditor.config.editable) {
				return;
			}
			flowEditor.isDrag = true;//--zili
			var o = (x + F);
			var G = (v + r);
			q.x = o - _state.margin;
			q.y = G - _state.margin;
			//--zili
			u.setPos();
			//B();
		};
		var z = function() {
			x = t.attr("x");
			v = t.attr("y");
			t.attr({
				opacity : 0.5
			});
			e.attr({
				opacity : 0.5
			});
			f.attr({
				opacity : 0.5
			});
		};
		var l = function() {
			flowEditor.isDrag = false;//--zili
			t.attr({
				opacity : 1
			});
			e.attr({
				opacity : 1
			});
			f.attr({
				opacity : 1
			});
			judge();
		};
		//----zili拖拽完成后，判断是否超限
       var judge = function(){
       	    var h = t.attr("height");
       	    var w = t.attr("width");
       	    var _minx = t.attr("x");
       	    var _maxx = _minx + w;
       	    var _miny =  t.attr("y");
       	    var _maxy = _miny+h;
       	    var isChange = false;
       	    var min_x = 0,min_y = 0,max_x = jQ(flowEditor.root).width(),max_y = jQ(flowEditor.root).height();
       	    if(_minx<0){
       	    	u.q.x = min_x;
       	    	isChange = true;
       	    }else if(_maxx>max_x){
       	    	u.q.x = max_x-w-_state.margin*2;
       	    	isChange = true;
       	    }
       	    if(_miny<0){
       	    	u.q.y = min_y;
       	    	isChange = true;
       	    }else if(_maxy>max_y){
       	    	u.q.y = max_y -h-_state.margin*2;
       	    	isChange = true;
       	    }
       	    if(isChange){
       	    	u.setPos();
       	    }
       }
		var s, i = {}, h = 5, q = {
			x : _state.attr.x - _state.margin,
			y : _state.attr.y - _state.margin,
			width : _state.attr.width + _state.margin * 2,
			height : _state.attr.height + _state.margin * 2
		};
		s = _raphael.path("M0 0L1 1").hide();
		i.t = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "s-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "t");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "t");
		}, function() {
		});
		i.lt = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "nw-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "lt");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "lt");
		}, function() {
		});
		i.l = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "w-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "l");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "l");
		}, function() {
		});
		i.lb = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "sw-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "lb");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "lb");
		}, function() {
		});
		i.b = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "s-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "b");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "b");
		}, function() {
		});
		i.rb = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "se-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "rb");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "rb");
		}, function() {
		});
		i.r = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "w-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "r");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "r");
		}, function() {
		});
		i.rt = _raphael.rect(0, 0, h, h).attr({
			fill : "#000",
			stroke : "#fff",
			cursor : "ne-resize"
		}).hide().drag(function(r, o) {
			D(r, o, "rt");
		}, function() {
			k(this.attr("x") + h / 2, this.attr("y") + h / 2, "rt");
		}, function() {
		});
		var D = function(F, r, G) {
			if (!flowEditor.config.editable) {
				return;
			}
			flowEditor.mouseIsMove = true;
			var o = _bx + F, H = _by + r;
			switch (G) {
			case "t":
				q.height += q.y - H;
				q.y = H;
				break;
			case "lt":
				q.width += q.x - o;
				q.height += q.y - H;
				q.x = o;
				q.y = H;
				break;
			case "l":
				q.width += q.x - o;
				q.x = o;
				break;
			case "lb":
				q.height = H - q.y;
				q.width += q.x - o;
				q.x = o;
				break;
			case "b":
				q.height = H - q.y;
				break;
			case "rb":
				q.height = H - q.y;
				q.width = o - q.x;
				break;
			case "r":
				q.width = o - q.x;
				break;
			case "rt":
				q.width = o - q.x;
				q.height += q.y - H;
				q.y = H;
				break;
			}
			//--zili
			u.setPos();
		};
		var k = function(r, o, F) {
			_bx = r;
			_by = o;
		};
		jQ([ t.node, f.node, n.node, e.node ]).bind("click",
			function() {
				if (!flowEditor.config.editable) {
					return;
				}
				//w();
				u.showActive();//--zili
				var o = jQ(_raphael).data("mod");
				switch (o) {
				case "pointer":
					break;
				case "path":
					var r = jQ(_raphael).data("currNode");
					if (r && r.getId() != id
							&& r.getId().substring(0, 4) == "rect") {
						jQ(_raphael).trigger("addpath", [ r, u ]);
					}
					break;
				}
				jQ(_raphael).trigger("click", u);
				jQ(_raphael).data("currNode", u);
				return false;
			});
		//--zili添加右击事件开始    
		jQ([ t.node, f.node, n.node, e.node ]).bind(
				"contextmenu",function(ev){
			//如果有被选中的元素
			var oEvent = ev||event;
		    if(u.isActive){
		    	$("#rightMenu").show().css('left', oEvent.clientX+'px')
				.css('top',oEvent.clientY+'px').removeClass("pathMenu");
				//flowEditor.isSelect = false;
		    }
		    return false;
		});
		var j = function(o, r) {
			//--解决在画路径的时候，重新选择路径，当前对象依然还存在，以致于画路径很麻烦。
			jQ(raphael).data("currNode", null);
			if(!jQ("#rightMenu").is(":hidden")){
         	  jQ("#rightMenu").hide();
         	}
			if (!flowEditor.config.editable) {
				return;
			}
			if (r.getId() == id) {
				jQ(_raphael).trigger("showprops", [ _state.props, r ]);
			} else {
				//d();
				//--zili
				if(!flowEditor.isSelect){
					u.hideActive();
				}
			}
		};
	    jQ(_raphael).bind("click", j);
		var c = function(o, F, r) {
			if (r.getId() == id) {
				if(F==''){
					F=' ';
				}
				f.attr({
					text : F
				});
				f.attr('text',$.trim(f.attr('text')));
			}
		};
		jQ(_raphael).bind("textchange", c);
		this.toJson = function() {
			var r = "{type:'" + _state.type + "',text:{text:'" + f.attr("text")
					+ "'}, attr:{ x:" + Math.round(t.attr("x")) + ", y:"
					+ Math.round(t.attr("y")) + ", width:"
					+ Math.round(t.attr("width")) + ", height:"
					+ Math.round(t.attr("height")) + "}, props:{";
			for ( var o in _state.props) {
				r += o + ":{value:'" + _state.props[o].value;
				if (o == "assignee") {

					r += "', actorNUM:'" + _state.props[o].actorNUM
							+ "',doRule:'" + _state.props[o].doRule
							+ "',assignerActivity:'"
							+ _state.props[o].assignerActivity
							+ "', actorModel:'" + _state.props[o].actorModel;

				}
				r += "'},";
			}
			if (r.substring(r.length - 1, r.length) == ",") {
				r = r.substring(0, r.length - 1);
			}
			r += "}}";
			return r;
		};
		this.restore = function(o) {
			var r = o;
			_state = jQ.extend(true, _state, o);
			f.attr({
				text : r.text.text
			});
			//--zili
			u.setPos();
		};
		this.getBBox = function() {
			return q;
		};
		this.getId = function() {
			return id;
		};
		//--删除元素
		this.remove = function() {
			t.remove();
			f.remove();
			n.remove();
			e.remove();
			s.remove();
			for ( var o in i) {
				i[o].remove();
			}
		};
		
		this.text = function() {
			return f.attr("text");
		};
		this.attr = function(o) {
			if (o) {
				t.attr(o);
			}
		};
		this.type = function() {
			return _state.type;
		};
		//---------------------zili代码---------------------start
		//--激活状态
		this.showActive = function() {
			this.s.show();
			this.isActive = true;
			for ( var o in this.i) {
				this.i[o].show();
			}
		}
		//--取消激活状态
		this.hideActive = function() {
			this.isActive = false;
			this.s.hide();
			for ( var o in this.i) {
				this.i[o].hide();
			}
		}
		//--获取更多rect的信息
		this.getMore = function(){
			this._state = _state;
			this.q = q;
			this.t = t;
			this.i = i;
			this.n = n;
			this.f = f;
			this.e = e;
			this.s = s;
			return this;
		};
		//--移动路径
		var showPath = function(q){
    		return "M" + q.x + " " + q.y + "L" + q.x + " " + (q.y + q.height)
				+ "L" + (q.x + q.width) + " " + (q.y + q.height) + "L"
				+ (q.x + q.width) + " " + q.y + "L" + q.x + " " + q.y;
        }
		 //--移动页面元素
         this.setPos =  function() {
         	u = u.getMore();//--
			var F = this.q.x + this._state.margin, 
			r = this.q.y + this._state.margin,G = this.q.width
					- this._state.margin * 2, o = this.q.height - this._state.margin * 2;
			this.t.attr({
				x : F,
				y : r,
				width : G,
				height : o
			});
			switch (this._state.showType) {
			case "image":
				e.attr({
					x : F + (G -this._state.img.width) / 2,
					y : r + (o - this._state.img.height) / 2
				}).show();
				break;
			case "text":
				this.t.show();
				this.f.attr({
					x : F + G / 2,
					y : r + o / 2
				}).show();
				break;
			case "image&text":
				this.t.show();
				this.n.attr({
					x : F + this._state.img.width + (G - this._state.img.width) / 2,
					y : r + flowEditor.config.lineHeight / 2
				}).show();
				this.f.attr(
						{
							x : F + this._state.img.width + (G - this._state.img.width)
									/ 2,
							y : r + (o - flowEditor.config.lineHeight) / 2
									+ flowEditor.config.lineHeight
						}).show();
				this.e.attr({
					x : F + this._state.img.width / 2,
					y : r + (o - this._state.img.height) / 2
				}).show();
				break;
			}
			this.i.t.attr({
				x : this.q.x + this.q.width / 2 - h / 2,
				y : this.q.y - h / 2
			});
			this.i.lt.attr({
				x : this.q.x - h / 2,
				y : this.q.y - h / 2
			});
			this.i.l.attr({
				x : this.q.x - h / 2,
				y : this.q.y - h / 2 + this.q.height / 2
			});
			this.i.lb.attr({
				x : this.q.x - h / 2,
				y : this.q.y - h / 2 + this.q.height
			});
			this.i.b.attr({
				x : this.q.x - h / 2 + this.q.width / 2,
				y : this.q.y - h / 2 + this.q.height
			});
			this.i.rb.attr({
				x : this.q.x - h / 2 + this.q.width,
				y : this.q.y - h / 2 + this.q.height
			});
			this.i.r.attr({
				x : this.q.x - h / 2 + this.q.width,
				y : this.q.y - h / 2 + this.q.height / 2
			});
			this.i.rt.attr({
				x : this.q.x - h / 2 + this.q.width,
				y : this.q.y - h / 2
			});
			this.s.attr({
				path : showPath(this.q)
			});
		   jQ(_raphael).trigger("rectresize", this);
		}
		u.setPos();
		//-----------------zili代码-------------------end
	};
	flowEditor.path = function(q, n, u, e) {

		var v = this, z = n, B = jQ.extend(true, {}, flowEditor.config.path), i, t, f, h = B.textPos, y, w, k = u, s = e, g = "path"
				+ flowEditor.util.nextId(), x;
		function p(G, H, D, L) {
			var F = this, M = G, r, o = D, O = L, K, I, N = H;
			switch (M) {
			case "from":
				r = z.rect(H.x - B.attr.fromDot.width / 2,
						H.y - B.attr.fromDot.height / 2, B.attr.fromDot.width,
						B.attr.fromDot.height).attr(B.attr.fromDot);
				break;
			case "big":
				r = z.rect(H.x - B.attr.bigDot.width / 2,
						H.y - B.attr.bigDot.height / 2, B.attr.bigDot.width,
						B.attr.bigDot.height).attr(B.attr.bigDot);
				break;
			case "small":
				r = z.rect(H.x - B.attr.smallDot.width / 2,
						H.y - B.attr.smallDot.height / 2,
						B.attr.smallDot.width, B.attr.smallDot.height).attr(
						B.attr.smallDot);
				break;
			case "to":
				r = z.rect(H.x - B.attr.toDot.width / 2,
						H.y - B.attr.toDot.height / 2, B.attr.toDot.width,
						B.attr.toDot.height).attr(B.attr.toDot);
				break;
			}
			if (r && (M == "big" || M == "small")) {
				r.drag(function(Q, P) {
					C(Q, P);
				}, function() {
					J();
				}, function() {
					E();
				});
				var C = function(R, Q) {
					var P = (K + R), S = (I + Q);
					F.moveTo(P, S);
				};
				var J = function() {
					if (M == "big") {
						K = r.attr("x") + B.attr.bigDot.width / 2;
						I = r.attr("y") + B.attr.bigDot.height / 2;
					}
					if (M == "small") {
						K = r.attr("x") + B.attr.smallDot.width / 2;
						I = r.attr("y") + B.attr.smallDot.height / 2;
					}
				};
				var E = function() {
				};
			}
			this.type = function(P) {
				if (P) {
					M = P;
				} else {
					return M;
				}
			};
			this.node = function(P) {
				if (P) {
					r = P;
				} else {
					return r;
				}
			};
			this.left = function(P) {
				if (P) {
					o = P;
				} else {
					return o;
				}
			};
			this.right = function(P) {
				if (P) {
					O = P;
				} else {
					return O;
				}
			};
			this.remove = function() {
				o = null;
				O = null;
				r.remove();
			};
			this.pos = function(P) {
				if (P) {
					N = P;
					r.attr({
						x : N.x - r.attr("width") / 2,
						y : N.y - r.attr("height") / 2
					});
					return this;
				} else {
					return N;
				}
			};
			this.moveTo = function(Q, T) {
				this.pos({
					x : Q,
					y : T
				});
				switch (M) {
				case "from":
					if (O && O.right() && O.right().type() == "to") {
						O.right()
								.pos(flowEditor.util.connPoint(s.getBBox(), N));
					}
					if (O && O.right()) {
						O.pos(flowEditor.util.center(N, O.right().pos()));
					}
					break;
				case "big":
					if (O && O.right() && O.right().type() == "to") {
						O.right()
								.pos(flowEditor.util.connPoint(s.getBBox(), N));
					}
					if (o && o.left() && o.left().type() == "from") {
						o.left().pos(flowEditor.util.connPoint(k.getBBox(), N));
					}
					if (O && O.right()) {
						O.pos(flowEditor.util.center(N, O.right().pos()));
					}
					if (o && o.left()) {
						o.pos(flowEditor.util.center(N, o.left().pos()));
					}
					var S = {
						x : N.x,
						y : N.y
					};
					if (flowEditor.util.isLine(o.left().pos(), S, O.right()
							.pos())) {
						M = "small";
						r.attr(B.attr.smallDot);
						this.pos(S);
						var P = o;
						o.left().right(o.right());
						o = o.left();
						P.remove();
						var R = O;
						O.right().left(O.left());
						O = O.right();
						R.remove();
					}
					break;
				case "small":
					if (o && O && !flowEditor.util.isLine(o.pos(), {
						x : N.x,
						y : N.y
					}, O.pos())) {
						M = "big";
						r.attr(B.attr.bigDot);
						var P = new p("small", flowEditor.util.center(o.pos(),
								N), o, o.right());
						o.right(P);
						o = P;
						var R = new p("small", flowEditor.util.center(O.pos(),
								N), O.left(), O);
						O.left(R);
						O = R;
					}
					break;
				case "to":
					if (o && o.left() && o.left().type() == "from") {
						o.left().pos(flowEditor.util.connPoint(k.getBBox(), N));
					}
					if (o && o.left()) {
						o.pos(flowEditor.util.center(N, o.left().pos()));
					}
					break;
				}
				m();
			};
		}
		function j() {
			var D, C, E = k.getBBox(), F = s.getBBox(), r, o;
			r = flowEditor.util.connPoint(E, {
				x : F.x + F.width / 2,
				y : F.y + F.height / 2
			});
			o = flowEditor.util.connPoint(F, r);
			D = new p("from", r, null, new p("small", {
				x : (r.x + o.x) / 2,
				y : (r.y + o.y) / 2
			}));
			D.right().left(D);
			C = new p("to", o, D.right(), null);
			D.right().right(C);
			this.toPathString = function() {
				if (!D) {
					return "";
				}
				var J = D, I = "M" + J.pos().x + " " + J.pos().y, H = "";
				while (J.right()) {
					J = J.right();
					I += "L" + J.pos().x + " " + J.pos().y;
				}
				var G = flowEditor.util.arrow(J.left().pos(), J.pos(),
						B.attr.arrow.radius);
				H = "M" + G[0].x + " " + G[0].y + "L" + G[1].x + " " + G[1].y
						+ "L" + G[2].x + " " + G[2].y + "z";
				return [ I, H ];
			};
			this.toJson = function() {
				var G = "[", H = D;
				while (H) {
					if (H.type() == "big") {
						G += "{x:" + Math.round(H.pos().x) + ",y:"
								+ Math.round(H.pos().y) + "},";
					}
					H = H.right();
				}
				if (G.substring(G.length - 1, G.length) == ",") {
					G = G.substring(0, G.length - 1);
				}
				G += "]";
				return G;
			};
			this.restore = function(H) {
				var I = H, J = D.right();
				for ( var G = 0; G < I.length; G++) {
					J.moveTo(I[G].x, I[G].y);
					J.moveTo(I[G].x, I[G].y);
					J = J.right();
				}
				this.hide();
			};
			this.fromDot = function() {
				return D;
			};
			this.toDot = function() {
				return C;
			};
			this.midDot = function() {
				var H = D.right(), G = D.right().right();
				while (G.right() && G.right().right()) {
					G = G.right().right();
					H = H.right();
				}
				return H;
			};
			this.show = function() {
				v.isActive = true;
				var G = D;
				while (G) {
					G.node().show();
					G = G.right();
				}
			};
			this.hide = function() {
				v.isActive = false;
				var G = D;
				while (G) {
					G.node().hide();
					G = G.right();
				}
			};
			this.remove = function() {
				var G = D;
				while (G) {
					if (G.right()) {
						G = G.right();
						G.left().remove();
					} else {
						G.remove();
						G = null;
					}
				}
			};
		}
		B = jQ.extend(true, B, q);
		i = z.path(B.attr.path.path).attr(B.attr.path);
		t = z.path(B.attr.arrow.path).attr(B.attr.arrow);
		x = new j();
		x.hide();
		f = z.text(0, 0, B.text.text).attr(B.text).attr(
				{
					text : B.text.text.replace("{from}", k.text()).replace(
							"{to}", s.text())
				});
		f.drag(function(r, o) {
			if (!flowEditor.config.editable) {
				return;
			}
			f.attr({
				x : y + r,
				y : w + o
			});
		}, function() {
			y = f.attr("x");
			w = f.attr("y");
		}, function() {
			var o = x.midDot().pos();
			h = {
				x : f.attr("x") - o.x,
				y : f.attr("y") - o.y
			};
		});
		m();
		jQ([ i.node, t.node ]).bind("click", function() {
			if (!flowEditor.config.editable) {
				return;
			}
			jQ(z).trigger("click", v);
			jQ(z).data("currNode", v);
			return false;
		});
		//--path右击删除
		jQ([ i.node, t.node ]).bind(			
				"contextmenu",function(ev){
				var oEvent = ev||event;
				var p = jQ(z).data("currNode");
				if (p.getId()==v.getId()) {
					$("#rightMenu").show().css('left', oEvent.clientX+'px')
					.css('top',oEvent.clientY+'px').addClass("pathMenu");
			  }
		    return false;
		});
		
		
		var l = function(r, C) {
			if (!flowEditor.config.editable) {
				return;
			}
			if (C && C.getId() == g) {
				x.show();
				jQ(z).trigger("showprops", [ B.props, v ]);
			} else {
				x.hide();
			}
			var o = jQ(z).data("mod");
			switch (o) {
			case "pointer":
				break;
			case "path":
				break;
			}
			
		};
		//--激活path
		var activePath = function(r, C) {
			if (!flowEditor.config.editable) {
				return;
			}
			x.show();
		};
		jQ(z).bind("click", l);
		jQ(v).bind("activePath", activePath);
		var A = function(o, r) {
			if (!flowEditor.config.editable) {
				return;
			}
			if (r && (r.getId() == k.getId() || r.getId() == s.getId())) {
				jQ(z).trigger("removepath", v);
			}
		};
		jQ(z).bind("removerect", A);
		//---当有了路径之后移动元素调用的方法
		var d = function(C, D) {
			if (!flowEditor.config.editable) {
				return;
			}
			if (k && k.getId() == D.getId()) {
				var o;
				if (x.fromDot().right().right().type() == "to") {
					o = {
						x : s.getBBox().x + s.getBBox().width / 2,
						y : s.getBBox().y + s.getBBox().height / 2
					};
				} else {
					o = x.fromDot().right().right().pos();
				}
				var r = flowEditor.util.connPoint(k.getBBox(), o);
				x.fromDot().moveTo(r.x, r.y);
				m();
				
			}
			if (s && s.getId() == D.getId()) {
				var o;
				if (x.toDot().left().left().type() == "from") {
					o = {
						x : k.getBBox().x + k.getBBox().width / 2,
						y : k.getBBox().y + k.getBBox().height / 2
					};
				} else {
					o = x.toDot().left().left().pos();
				}
				var r = flowEditor.util.connPoint(s.getBBox(), o);
				x.toDot().moveTo(r.x, r.y);
				m();
			}
		};
		jQ(z).bind("rectresize", d);
		var c = function(r, o, C) {
			if (C.getId() == g) {
			   if(o==''){
				 	o=' ';
				 }
				f.attr({
					text : o
				});
				f.attr('text',$.trim(f.attr('text')));
			}
		};
		jQ(z).bind("textchange", c);
		this.from = function() {
			return k;
		};
		this.to = function() {
			return s;
		};
		this.toJson = function() {
			var r = "{from:'" + k.getId() + "',to:'" + s.getId() + "', dots:"
					+ x.toJson() + ",text:{text:'" + f.attr("text")
					+ "'},textPos:{x:" + Math.round(h.x) + ",y:"
					+ Math.round(h.y) + "}, props:{";
			for ( var o in B.props) {
				var _name = "";//--有的路径必须有名字  by  zili  2015-05-05
				if(this.named){
					_name = B.props[o].value||this.named;
				}else{
					_name = "";
				}
				r += o + ":{value:'" + _name + "'},";
			}
			if (r.substring(r.length - 1, r.length) == ",") {
				r = r.substring(0, r.length - 1);
			}
			r += "}}";
			return r;
		};
		this.restore = function(o) {
			var r = o;
			B = jQ.extend(true, B, o);
			x.restore(r.dots);
		};
		this.remove = function() {
			x.remove();
			i.remove();
			t.remove();
			f.remove();
			try {
				jQ(z).unbind("click", l);
			} catch (o) {
			}
			try {
				jQ(z).unbind("removerect", A);
			} catch (o) {
			}
			try {
				jQ(z).unbind("rectresize", d);
			} catch (o) {
			}
			try {
				jQ(z).unbind("textchange", c);
			} catch (o) {
			}
		};

		function m() {
			var r = x.toPathString(), o = x.midDot().pos();
			i.attr({
				path : r[0]
			});
			t.attr({
				path : r[1]
			});
			f.attr({
				x : o.x + h.x,
				y : o.y + h.y
			});
		}

		this.getId = function() {
			return g;
		};
		this.text = function() {
			return f.attr("text");
		};
		this.attr = function(o) {
			if (o && o.path) {
				i.attr(o.path);
			}
			if (o && o.arrow) {
				t.attr(o.arrow);
			}
		};
		
		//--取消激活状态 add Xuejw 2016-11-11
		this.hideActive = function() {
			x.hide();
		}
	};
	flowEditor.props = function(h, f) {
		var c = jQ("#myflow_props").hide().draggable({
			handle : "#myflow_props_handle"
		}).resizable().css(flowEditor.config.props.attr).bind("click",
				function() {
					return false;
				}), e = c.find("table"), g = f, i;
		var d = function(n, m, o) {
			if (i && i.getId() == o.getId()) {
				return;
			}
			i = o;
			jQ(e).find(".editor").each(function() {
				var k = jQ(this).data("editor");
				if (k) {
					k.destroy();
				}
			});
			e.empty();
			c.show();
			for ( var l in m) {
				e.append("<tr><th>" + m[l].label + '</th><td><div id="p' + l
						+ '" class="editor"></div></td></tr>');
				if (m[l].editor) {
					m[l].editor().init(m, l, "p" + l, o, g);
				}
			}
		};
		jQ(g).bind("showprops", d);
	};

	flowEditor.editors = {
		textEditor : function() {
			var d, e, c, g, f;
			this.init = function(i, h, m, l, j) {
				d = i;
				e = h;
				c = m;
				g = l;
				f = j;
				jQ('<input  class="form-control"/>').val(g.text()).change(
						function() {
							i[e].value = jQ(this).val();
							jQ(f).trigger("textchange", [ jQ(this).val(), g ]);
						}).appendTo("#" + c);
				jQ("#" + c).data("editor", this);
			};
			this.destroy = function() {
				jQ("#" + c + " input").each(function() {
					d[e].value = jQ(this).val();
					jQ(f).trigger("textchange", [ jQ(this).val(), g ]);
				});
			};
		}

	};
	flowEditor.init = function(root, conf) {
		
		flowEditor.mouseIsMove = "init";
		//--sy
		var width = jQ(window).width()- 100-20-20;
		var height = jQ(window).height()-40;
		jQ(root).parent().width(width);
		jQ(root).parent().height(height);
		var raphael = Raphael(root, width-26, height-20);
		flowEditor.root = root;
		jQ(root).width(width-26);
		jQ(root).height(height-20);
		//--sy end
		var rArray = {}, pArray = {};
		jQ.extend(true, flowEditor.config, conf);
		jQ(document).keydown(function(i) {
			if (!flowEditor.config.editable) {
				return;
			}
			if (i.keyCode == 46) {//删除
				var j = jQ(raphael).data("currNode");
				if (j) {
					if (j.getId().substring(0, 4) == "rect") {
						jQ(raphael).trigger("removerect", j);
					} else {
						if (j.getId().substring(0, 4) == "path") {
							jQ(raphael).trigger("removepath", j);
						}
					}
					jQ(raphael).removeData("currNode");
				}
			}
		});
		jQ("#myflow").children().click(function(e) {
         	if(!jQ("#rightMenu").is(":hidden")){
         	  jQ("#rightMenu").hide();
         	}
         	/**
         	 * 获取当前选中控件
         	 */
         	var actives = getActives();
         	if (actives.length>0&&!flowEditor.mouseIsMove) {
         		for(var a = 0;a<actives.length;a++){
         			var u = actives[a];
         			u.hideActive();
         		}
         	}
         	if(e.target.nodeName=="svg"){
         		jQ(raphael).trigger("showprops", [ flowEditor.config.props.props, {
         			getId : function() {
         				return "00000000";
         			}
         		} ]);
         	}
		});
		var removeFun = function(c, i) {
			if (!flowEditor.config.editable) {
				return;
			}
			if (i.getId().substring(0, 4) == "rect") {
				rArray[i.getId()] = null;
				i.remove();
			} else {
				if (i.getId().substring(0, 4) == "path") {
					pArray[i.getId()] = null;
					i.remove();
				}
			}
		};
		jQ(raphael).bind("removepath", removeFun);
		jQ(raphael).bind("removerect", removeFun);
		//-----------------zili代码开始---------------------------
		//--右击删除
         jQ(".rightMenu").find(".delElement").bind("click",function(ev){
         	var j = jQ(raphael).data("currNode");
         	var actives = getActives();
			if (actives.length>0) {
			   for(var a = 0;a<actives.length;a++){
					var u = actives[a];
					if (u.getId().substring(0, 4) == "rect") {
					jQ(raphael).trigger("removerect", u);
				    } else {
					if (u.getId().substring(0, 4) == "path") {
						jQ(raphael).trigger("removepath", u);
					}
				  }
				  u.remove();
				  rArray[u.getId()] = null;
				}
			}
			jQ(raphael).removeData("currNode");
         	var oDiv = jQ("#rightMenu");
         	oDiv.hide();
         });
         //--右击左对齐
          jQ(".rightMenu").find(".turnLeft").bind("click",function(ev){
           	 setAlign("left");
           })
           //--右击右对齐
          jQ(".rightMenu").find(".turnRight").bind("click",function(ev){
           	 setAlign("right");
           })
           //--右击上对齐
          jQ(".rightMenu").find(".turnTop").bind("click",function(ev){
           	 setAlign("top");
           })
           //--右击下对齐
          jQ(".rightMenu").find(".turnBottom").bind("click",function(ev){
           	 setAlign("bottom");
           })
          //--对齐的共同方法
          function setAlign(direction){
        	var oDiv = jQ("#rightMenu");
        	if(oDiv.hasClass('pathMenu')){
        		return ;
        	}
        	oDiv.hide();
          	var actives = getRectActives();
      	    var standard = getStandardPos(actives,direction);
       	    var dest ;
       	    if(direction=="left"||direction=="right"){
       	    	dest = "y";
       	    }else{
       	    	dest = "x";
       	    }
       	    for(var u = 0;u<actives.length;u++){
	       	  	var _r = actives[u];
	       	  	if((_r.q[dest]+_r._state.margin)!=standard){
	       	  		_r.q[dest] = standard-_r._state.margin;
	       	  		_r.setPos();
	       	  	}
       	     }
       	    
          }
         //--获取选中的rect
         function getRectActives(){
         	var result = [];
         	for(var a in rArray){
         		var rs = rArray[a];
         		if(rs&&rs.isActive){
         			result.push(rs);
         		}
         	}
         	return result;
         }
         //--获取选中的元素
         function getActives(){
          	var result = [];
          	for(var a in rArray){
          		var rs = rArray[a];
          		if(rs&&rs.isActive){
          			result.push(rs);
          		}
          	}
          	for(var a in pArray){
          		var ps = pArray[a];
          		if(ps&&ps.isActive){
          			result.push(ps);
          		}
          	}
          	return result;
          }
         //--获取对齐标准的x坐标或者y坐标
        function  getStandardPos(actives,direction){
        	if(direction=="left"||direction=="right"){
			      actives.sort(function(a,b){
			        return a.t.attr("x")>b.t.attr("x")?1:-1;
			      })
			      if(direction=="left"){
			      	 return actives[0].t.attr("y");
			      }else{
			      	 return actives[actives.length-1].t.attr("y");
			      }
        	}else{
        		  actives.sort(function(a,b){
			        return a.t.attr("y")>b.t.attr("y")?1:-1;
			      })
			      if(direction=="top"){
			      	 return actives[0].t.attr("x");
			      }else{
			      	 return actives[actives.length-1].t.attr("x");
			      }
           }
       }
		//--框选开始
		document.onmousedown = function(){
			flowEditor.mouseIsMove = false;
			var evt = window.event || arguments[0];
			var dis_x = jQ(root).offset().left;
			var dis_y = jQ(root).offset().top;
			var start_x = evt.clientX,start_y = evt.clientY,
			end_x = null,end_y = null;
			var selDiv = document.createElement("div");
			var tar = evt.srcElement||evt.target;
			var _svg = jQ(root).find(":first-child");
			if(tar.tagName!="DIV"&&(tar.tagName=="svg"||tar.tagName=="svg")){
				flowEditor.isSelect = true;
		        selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
		        selDiv.id = "selectDiv";
		        document.body.appendChild(selDiv);
				selDiv.style.left = start_x + "px";
				selDiv.style.top = start_y + "px";
				clearEventBubble(evt);
				document.onmousemove = function(){
					evt = window.event || arguments[0];
					if(!flowEditor.isDrag&&flowEditor.isSelect&&(tar.tagName=="svg")){
						end_x = evt.clientX;
				        end_y = evt.clientY;
		                if (selDiv.style.display == "none") {
					      selDiv.style.display = "";
				        }
						selDiv.style.left = Math.min(end_x, start_x) + "px";
						selDiv.style.top = Math.min(end_y, start_y) + "px";
						selDiv.style.width = Math.abs(end_x - start_x) + "px";
						selDiv.style.height = Math.abs(end_y - start_y) + "px";
					    var _l = selDiv.offsetLeft, _t = selDiv.offsetTop;
				        var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;
		               for(var rs in rArray){
	                	 var _rect = rArray[rs];
	                	 if(_rect){
	                		var w = _rect.t.attr("width");
							var h = _rect.t.attr("height");
							var pos = {};
		                	pos.leftT_x = _rect.t.attr("x")+dis_x;pos.leftT_y = _rect.t.attr("y")+dis_y;
							pos.rightB_x = pos.leftT_x+w;pos.rightB_y = pos.leftT_y+h;
							if (pos.rightB_x > _l && pos.rightB_y > _t && pos.leftT_x < _l + _w && pos.rightB_y< _t + _h) {
								_rect.showActive();
								flowEditor.mouseIsMove = true;
							}
		                 }
		               }
		               for(var p in pArray){
		            	   var _path = pArray[p];
		                	 if(_path){
		                		 if(_path.from().isActive&&_path.to().isActive){
		                			jQ(_path).trigger("activePath");
		                			flowEditor.mouseIsMove = true;
		                		}
			                 }
		               }
					}
					clearEventBubble(evt);
				};
				document.onmouseup =  function(){
					evt = window.event || arguments[0];
					document.onmousemove = null;
	 				flowEditor.isSelect =  false;
					if(selDiv) {
						jQ("#selectDiv").remove();
			        }
				}
		    }
		}
		function clearEventBubble(evt) {
			if (evt.stopPropagation)
				evt.stopPropagation();
			else
				evt.cancelBubble = true;
			if (evt.preventDefault)
				evt.preventDefault();
			else
				evt.returnValue = false;
       }
		//-------------------zili代码结束---------------------------
		//--从左侧工具栏拖动后调用的方法
		var addRectFun = function(j, c, k) {
			var i = new flowEditor.rect(jQ.extend(true, {},
					flowEditor.config.tools.states[c], k), raphael);
			rArray[i.getId()] = i;
		};
		jQ(raphael).bind("addrect", addRectFun);

		var addPathFun = function(i, k, j) {
			var c = new flowEditor.path({}, raphael, k, j);
			pArray[c.getId()] = c;
		};
		jQ(raphael).bind("addpath", addPathFun);

		jQ(raphael).data("mod", "point");
		jQ(raphael).data("activities", rArray);

		if (flowEditor.config.editable) {

			jQ('#myflow_actor_setting').draggable({
				//handle : "#setting_head"//注释掉为了也可以拖动内容区
			});
            //--sy
			/*jQ("#myflow_tools").draggable({
				//handle : "#myflow_tools_handle"//注释掉为了也可以拖动内容区
			}).css(flowEditor.config.tools.attr);*/
			//--sy end
			jQ("#myflow_tools .node").hover(function() {
				jQ(this).addClass("mover");
			}, function() {
				jQ(this).removeClass("mover");
			});
			jQ("#myflow_tools .selectable").click(function() {
				jQ(".selected").removeClass("selected");
				jQ(this).addClass("selected");
				jQ(raphael).data("mod", this.id);
			});
			jQ("#myflow_tools .state").each(function() {
				jQ(this).draggable({
					helper : "clone"
				});
			});
			jQ(root).droppable({
				accept : ".state",
				drop : function(c, i) {
					var dis_x = jQ(root).offset().left;
			        var dis_y = jQ(root).offset().top;
					jQ(raphael).trigger("addrect", [ i.helper.attr("type"), {
						attr : {
							x : i.helper.offset().left-dis_x,
							y : i.helper.offset().top-dis_y
						}
					} ]);
				}
			}).resizable({	//-sy	
				handles: "e,s,w,n,se",
				helper: "ui-resizable-helper",
				stop: function(event, ui) { 
					var that = $(this).data("ui-resizable");
					console.log(that.axis);
					
					var minX,minY,maxX,maxY;//--画布上矩形的极点
					var minWidth,minHeight;//--画布的最小宽高
					  for(var r in rArray){
					    	if(rArray[r]){
								minX = minX?(rArray[r].getBBox().x<minX?rArray[r].getBBox().x:minX):rArray[r].getBBox().x;
								minY = minY?(rArray[r].getBBox().y<minY?rArray[r].getBBox().y:minY):rArray[r].getBBox().y;
								maxX = maxX?(rArray[r].getBBox().x+rArray[r].getBBox().width>maxX?(rArray[r].getBBox().x+rArray[r].getBBox().width):maxX):rArray[r].getBBox().x+rArray[r].getBBox().width;
								maxY = maxY?(rArray[r].getBBox().y+rArray[r].getBBox().height>maxY?rArray[r].getBBox().y+rArray[r].getBBox().height:maxY):rArray[r].getBBox().y+rArray[r].getBBox().height;
					    	}
					  }
					  var b_x = $(this).offset().left;
					  var b_y = $(this).offset().top;
					  if(maxX&&maxY){
						   minWidth = maxX;
						   minHeight = maxY;
					  }
					  /**
					   * 判断拖动是画布的哪个边
					   * 东 南 东南 拖动时 只需要判断判断画布不小于最小宽高
					   * 西 北 拖动时 需要减小画布上矩形的坐标  以及判断缩小的宽高差值不能超过minx miny  
					   * 同时扩大时画布不能超出div的边界
					   */
					  if(that.axis=="e"){
						  if(minWidth){
							  if(ui.size.width<minWidth){
								  raphael.setSize(minWidth,ui.originalSize.height);
								  $(ui.element).css("width",minWidth);
								  $(ui.element).css("height",ui.originalSize.height);
								
							  }else{
								  raphael.setSize(ui.size.width,ui.size.height);
								  $(ui.element).css("width",ui.size.width);
								  $(ui.element).css("height",ui.size.height);
							  }
						  }else{
							  raphael.setSize(ui.size.width,ui.size.height); 
							  $(ui.element).css("width",ui.size.width);
							  $(ui.element).css("height",ui.size.height);
						  }
					  }else if(that.axis=="s"){
						  if(minHeight){
							  if(ui.size.height<minHeight){
								  raphael.setSize(ui.originalSize.width,minHeight);
								  $(ui.element).css("width",ui.originalSize.width);
								  $(ui.element).css("height",minHeight);
							  }else{
								  raphael.setSize(ui.size.width,ui.size.height); 
								  $(ui.element).css("width",ui.size.width);
								  $(ui.element).css("height",ui.size.height);
							  }
						  }else{
							  raphael.setSize(ui.size.width,ui.size.height);
							  $(ui.element).css("width",ui.size.width);
							  $(ui.element).css("height",ui.size.height);
						  }
					  }else if(that.axis=="se"){
						  if(minHeight&&minWidth){
							  if(ui.size.height<minHeight||ui.size.width<minWidth){
								  raphael.setSize(ui.originalSize.width,ui.originalSize.height);
								  $(ui.element).css("width",ui.originalSize.width);
								  $(ui.element).css("height",ui.originalSize.height);
							  }else{
								  raphael.setSize(ui.size.width,ui.size.height); 
								  $(ui.element).css("width",ui.size.width);
								  $(ui.element).css("height",ui.size.height);
							  }
						  }else{
							  raphael.setSize(ui.size.width,ui.size.height); 
							  $(ui.element).css("width",ui.size.width);
							  $(ui.element).css("height",ui.size.height);
						  }
					  }else if(that.axis=="n"){
						  if(minY){
							if(ui.originalSize.height - ui.size.height > minY){
								raphael.setSize(ui.originalSize.width,ui.originalSize.height);
								$(ui.element).css("top",ui.originalPosition.top);
								$(ui.element).css("width",ui.originalSize.width);
								$(ui.element).css("height",ui.originalSize.height);
								
							}else{
								if(ui.position.top<30){
									raphael.setSize(ui.size.width,ui.size.height); 
									$(ui.element).css("top",30);
									$(ui.element).css("width",ui.size.width);
									$(ui.element).css("height",ui.size.height);
								}else{
									var offsetY = ui.originalSize.height - ui.size.height;
									minY = minY - offsetY;
									
									for(var r in rArray){
										 if(rArray[r]){
											var oldY = rArray[r].q.y;
											rArray[r].q.y = oldY-offsetY; 
											rArray[r].setPos();
										 }
									 }
									raphael.setSize(ui.originalSize.width,ui.size.height); 
									$(ui.element).css("top",ui.position.top);
									$(ui.element).css("width",ui.originalSize.width);
									$(ui.element).css("height",ui.size.height);
								}
								
							}
						  }else{
							  raphael.setSize(ui.size.width,ui.size.height); 
							  $(ui.element).css("top",ui.position.top);
							  $(ui.element).css("width",ui.originalSize.width);
							  $(ui.element).css("height",ui.size.height);
							
						  }
					  }else if(that.axis=="w"){
						  if(minX){
								if(ui.originalSize.width - ui.size.width > minX){
									raphael.setSize(ui.originalSize.width,ui.originalSize.height);
									$(ui.element).css("left",ui.originalPosition.left-130);
									$(ui.element).css("width",ui.originalSize.width);
									$(ui.element).css("height",ui.originalSize.height);
									
								}else{
									if(ui.position.left<130){
										raphael.setSize(ui.size.width,ui.size.height); 
										$(ui.element).css("left",'auto');
										$(ui.element).css("width",ui.size.width);
										$(ui.element).css("height",ui.originalSize.height);
									}else{
										var offsetX = ui.originalSize.width - ui.size.width;
										minX = minX - offsetX;
										
										for(var r in rArray){
											 if(rArray[r]){
												var oldX = rArray[r].q.x;
												rArray[r].q.x = oldX-offsetX; 
												rArray[r].setPos();
											 }
										 }
										raphael.setSize(ui.size.width,ui.originalSize.height); 
										$(ui.element).css("left",ui.position.left-130);
										$(ui.element).css("width",ui.size.width);
										$(ui.element).css("height",ui.originalSize.height);
									}
									
								}
						  }else{
							  raphael.setSize(ui.size.width,ui.size.height); 
							  $(ui.element).css("left",ui.position.left-130);
							  $(ui.element).css("width",ui.size.width);
							  $(ui.element).css("height",ui.originalSize.height);
							
						  }
				   }
				} 
			});
			
			//--sy end

			var getJson = function() {
				isHaveName();//--判断是否该有name
				var dataStr = "{states:{";
				for ( var r in rArray) {
					if (rArray[r]) {
						dataStr += rArray[r].getId() + ":" + rArray[r].toJson()
								+ ",";
					}
				}
				if (dataStr.substring(dataStr.length - 1, dataStr.length) == ",") {
					dataStr = dataStr.substring(0, dataStr.length - 1);
				}
				dataStr += "},paths:{";
				for ( var p in pArray) {
					if (pArray[p]) {
						dataStr += pArray[p].getId() + ":" + pArray[p].toJson()
								+ ",";
					}
				}
				if (dataStr.substring(dataStr.length - 1, dataStr.length) == ",") {
					dataStr = dataStr.substring(0, dataStr.length - 1);
				}
				dataStr += "},props:{props:{";
				for ( var prop in flowEditor.config.props.props) {
					var propsValue = flowEditor.config.props.props[prop].value;					
					dataStr += prop + ":{value:'"
							+ propsValue + "'},";
				}
				if (dataStr.substring(dataStr.length - 1, dataStr.length) == ",") {
					dataStr = dataStr.substring(0, dataStr.length - 1);
				}
				dataStr += "}}}";
				return dataStr;
			};
			/*--zili  2015-05-05
			判断路径是否该有name，两种情况必须有：
			①path的from是task，且其他path的from有该task的，则必须有name；
			②path的from是决定节点的，且其他path的from是有该决定节点的，则必须有name。
			③如果path的from是task，且该task只有一条path，则不能有name。
			④其他情况name可有可无，现都把它处理为没有name。和③视为一种情况。
			*/
            function isHaveName(){
             for(var a in pArray){
          		var ps = pArray[a];
          		if(ps){
          			ps.named = "";//置值
      				var _from = ps.from();
      				for(var b in pArray){
      					var temp = pArray[b];
      					if(temp){
  						  if(_from.type()=="task"&&temp.from().type()=="task"){
      						if(a!=b&&_from==temp.from()){
      							 console.log(_from);
      							 console.log(ps);
      							 ps.named = "TO "+ps.to()._state.props.text.value;
      						     break ;
      						}
      				        }else if(_from.type()=="decision"&&temp.from().type()=="decision"){
      						if(a!=b&&_from==temp.from()){
      							 ps.named = "TO "+ps.to()._state.props.text.value;
      						     break ;
      						}
      					   }
      					}
      				}
          		}
              }
           }
			jQ("#myflow_save").click(function() {
				var dataStr_ = getJson();
				flowEditor.config.tools.save.onclick(dataStr_);
			});

			jQ("#myflow_xml").click(function() {
				var dataStr_ = getJson();
				flowEditor.config.tools.xmlSrc.onclick(dataStr_);
			});
			
			jQ("#myflow_json").click(function() {
				var dataStr_ = getJson();
				flowEditor.config.tools.jsonSrc.onclick(dataStr_);
			});

			new flowEditor.props({}, raphael);
		}
		if (conf.restore) {
			var restore_ = conf.restore;
			var z = {};

			if (restore_.props) {
				jQ.extend(true, flowEditor.config.props.props.name,
						restore_.props.props.name);
				jQ.extend(true, flowEditor.config.props.props.key,
						restore_.props.props.key);
				jQ.extend(true, flowEditor.config.props.props.desc,
						restore_.props.props.desc);
			}

			if (restore_.states) {
				for ( var s in restore_.states) {
					var d = new flowEditor.rect(jQ.extend(true,{},
											flowEditor.config.tools.states[restore_.states[s].type],
											restore_.states[s]), raphael);
					d.restore(restore_.states[s]);
					z[s] = d;
					rArray[d.getId()] = d;
				}
			}
			if (restore_.paths) {
				for ( var p in restore_.paths) {
					var n = new flowEditor.path(jQ.extend(true, {},
							flowEditor.config.tools.path, restore_.paths[p]),
							raphael, z[restore_.paths[p].from],
							z[restore_.paths[p].to]);
					n.restore(restore_.paths[p]);
					pArray[n.getId()] = n;
				}
			}
		}
		var histRs = flowEditor.config.historyRects, actRs = flowEditor.config.activeRects;
		if (histRs.rects.length || actRs.rects.length) {
			var z = {};
			for ( var h in pArray) {
				if (!z[pArray[h].from().text()]) {
					z[pArray[h].from().text()] = {
						rect : pArray[h].from(),
						paths : {}
					};
				}
				z[pArray[h].from().text()].paths[pArray[h].text()] = pArray[h];
				if (!z[pArray[h].to().text()]) {
					z[pArray[h].to().text()] = {
						rect : pArray[h].to(),
						paths : {}
					};
				}
			}
			for ( var u = 0; u < histRs.rects.length; u++) {
				if (z[histRs.rects[u].name]) {
					z[histRs.rects[u].name].rect.attr(histRs.rectAttr);
				}
				for ( var t = 0; t < histRs.rects[u].paths.length; t++) {
					if (z[histRs.rects[u].name].paths[histRs.rects[u].paths[t]]) {
						z[histRs.rects[u].name].paths[histRs.rects[u].paths[t]]
								.attr(histRs.pathAttr);
					}
				}
			}
			for ( var u = 0; u < actRs.rects.length; u++) {
				if (z[actRs.rects[u].name]) {
					z[actRs.rects[u].name].rect.attr(actRs.rectAttr);
				}
				for ( var t = 0; t < actRs.rects[u].paths.length; t++) {
					if (z[actRs.rects[u].name].paths[actRs.rects[u].paths[t]]) {
						z[actRs.rects[u].name].paths[actRs.rects[u].paths[t]]
								.attr(actRs.pathAttr);
					}
				}
			}
		}
	};
	
	jQ.fn.myflow = function(conf) {
		return this.each(function() {
			flowEditor.init(this, conf);
		});
	};
	jQ.myflow = flowEditor;
})(jQuery);